package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.domain.BannerDomain;
import com.ngc.mall.domain.BannerItemDomain;
import com.ngc.mall.mapper.BannerMapper;
import com.ngc.mall.service.IBannerItemService;
import com.ngc.mall.service.IBannerService;
import com.ngc.mall.vo.BannerVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class BannerServiceImpl extends BaseServiceImpl<BannerMapper, BannerDomain> implements IBannerService {
    @Resource
    private IBannerItemService iBannerItemService;


    @Override
    public Long createBanner(BannerDomain domain){
        return super.create(domain);
    }

    @Override
    public List<BannerDomain> selectAllBanner(){
        return super.list();
    }

    @Override
    public Boolean updateBannerById(Long id,BannerDomain dto){
        return super.update(Wrappers.<BannerDomain>lambdaUpdate().eq(BannerDomain::getId,id).set(BannerDomain::getTemplate,dto.getTemplate()).set(BannerDomain::getIsEnabled,dto.getIsEnabled()).set(BannerDomain::getKeyword,dto.getKeyword()).set(BannerDomain::getName,dto.getName()).set(BannerDomain::getNote,dto.getNote()));
    }


    @Override
    public BannerVO getDetailById(Long id){
        BannerDomain bannerDomain=super.getById(id);
        List<BannerItemDomain> bannerItemDomainList=iBannerItemService.list(Wrappers.<BannerItemDomain>lambdaQuery().eq(BannerItemDomain::getBannerId,id).orderByAsc(BannerItemDomain::getSort));
        return BannerVO.builder().template(bannerDomain.getTemplate()).name(bannerDomain.getName()).note(bannerDomain.getNote()).isEnabled(bannerDomain.getIsEnabled()).keyword(bannerDomain.getKeyword()).bannerItemList(bannerItemDomainList).build();
    }

    @Override
    public Boolean updateStatusById(Long id, boolean isEnabled) {
        return super.update(Wrappers.<BannerDomain>lambdaUpdate().eq(BannerDomain::getId,id).set(BannerDomain::getIsEnabled,isEnabled));
    }

    @Override
    public List<BannerVO> getBannerByKeyword(List<String> keywords) {
        List<BannerVO> bannerVOList=new ArrayList<>();
        for(String keyword:keywords){
            Wrapper wrapper=Wrappers.<BannerDomain>lambdaQuery().eq(BannerDomain::getKeyword,keyword);
            BannerDomain bannerDomain=super.getOne(wrapper);
            List<BannerItemDomain> bannerItemDomainList=iBannerItemService.list(Wrappers.<BannerItemDomain>lambdaQuery().eq(BannerItemDomain::getBannerId,bannerDomain.getId()).orderByAsc(BannerItemDomain::getSort));
            bannerVOList.add(BannerVO.builder().template(bannerDomain.getTemplate()).name(bannerDomain.getName()).note(bannerDomain.getNote()).keyword(bannerDomain.getKeyword()).isEnabled(bannerDomain.getIsEnabled()).bannerItemList(bannerItemDomainList).build());
        }
        return bannerVOList;
    }

}
